package me.wcy.express.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;

import java.io.Console;

import me.wcy.express.R;
import me.wcy.express.http.HttpCallback;
import me.wcy.express.http.HttpClient;
import me.wcy.express.model.ApiResult;
import me.wcy.express.model.LoginEntity;
import me.wcy.express.utils.DataManager;
import me.wcy.express.utils.binding.Bind;
import me.wcy.express.utils.binding.ViewBinder;

public class LoginActivity extends AppCompatActivity {
    SharedPreferences settings;
    @Bind(R.id.uname)
    private EditText tv_username;
    @Bind(R.id.upass)
    private EditText tv_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)actionBar.setDisplayHomeAsUpEnabled(false);

        //取登录信息
        settings= PreferenceManager.getDefaultSharedPreferences(this);
        //检查APPID和APPSECRET
        boolean checkresult = CheckSetting();
        String username = settings.getString("username","");
        String password = settings.getString("password","");

        //如果自动登录，进行登录，直接跳转主界面,如果是注销返回
        boolean forced_logoff = getIntent().getBooleanExtra("force_logff",false);
        if (forced_logoff)
        {
            SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
            edit.putString("username","");
            edit.putString("password","");
            edit.apply();
        } else {
            tv_username.setText(username);
            tv_password.setText(password);
            //判断自动登录
            if (settings.getBoolean("autologin",false))
            {
                login(username,password);
            }
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = getLayoutInflater().inflate(layoutResID, null);
        setContentView(view);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        ViewBinder.bind(this);
    }

    /**
     * 登录
     * @param view
     */
    public void LoginClick(View view)
    {
        //检查是否设置了APPID和密钥
        if(!CheckSetting())return;

        String username = tv_username.getText().toString();
        String password = tv_password.getText().toString();
        //判断是否可以自动登录
        if (username.isEmpty() || password.isEmpty())
        {
            Toast.makeText(this,"请输入账号或者密码",Toast.LENGTH_SHORT).show();
            return;
        }

    }
    private void login(String username,String password)
    {
        //登录请求
        HttpClient.login(username, password, new HttpCallback<ApiResult<LoginEntity>>() {
            @Override
            public void onResponse(ApiResult<LoginEntity> loginEntityApiResult) {
                if (loginEntityApiResult.getCode() == 1) {
                    Intent express = new Intent(LoginActivity.this, ExpressActivity.class);
                    startActivity(express);
                    LoginActivity.this.finish();
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                String msg = volleyError.getLocalizedMessage();
                if (msg == null || msg.isEmpty())msg = "认证失败，请重新登录";
                String finalMsg = msg;
                runOnUiThread(() -> Toast.makeText(LoginActivity.this, finalMsg,Toast.LENGTH_SHORT).show());
            }
        });

    }
    private boolean CheckSetting()
    {
        if (settings.getString("appid","").isEmpty() || settings.getString("appsecret","").isEmpty())
        {
            Intent intent = new Intent();
            intent.setClass(LoginActivity.this,SettingActivity.class);
            startActivity(intent);
            Toast.makeText(this,R.string.setalert,Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    public void AutoLoginClick(View view) {
        CheckBox cb = (CheckBox)view;
        PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("autologin",cb.isChecked()).apply();

    }
}